const { Service } = require('dx-router-admin');
const { OpendbDevice } = require('dx-func-entity');

module.exports = class OpenDevice extends Service {
	constructor(ctx) {
		super(ctx);
		this.entity = OpendbDevice;
	}
	async saveDevice(pushClientId) {
		const {
			appId,
			deviceId,
			deviceBrand,
			deviceModel,
			osName,
			osVersion,
			osLanguage,
			osTheme,
			devicePixelRatio,
			windowWidth,
			windowHeight,
			screenWidth,
			screenHeight,
			romName,
			romVersion,
			PLATFORM,
			uniPlatform,
		} = this.$dx.request.context;
		let platform = PLATFORM || uniPlatform;
		const device = await this.where('device_id', deviceId).find();
		const now = Date.now();
		const data = {
			appid: appId,
			device_id: deviceId,
			vendor: deviceBrand,
			model: deviceModel,
			uni_platform: platform,
			os_name: osName,
			os_version: osVersion,
			os_language: osLanguage,
			os_theme: osTheme,
			pixel_ratio: devicePixelRatio,
			window_width: windowWidth,
			window_height: windowHeight,
			screen_width: screenWidth,
			screen_height: screenHeight,
			rom_name: romName,
			rom_version: romVersion,
			last_update_date: now,
			push_clientid: pushClientId,
		};
		if (!device) {
			data.create_date = now;
		} else {
			data._id = device._id;
		}
		await this.save(data);
	}
};
